package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.gcgl.mapper.GcBszbzfdMapper;
import com.ruoyi.gcgl.domain.GcBszbzfd;
import com.ruoyi.gcgl.domain.GcFyhx;
import com.ruoyi.gcgl.service.IGcBszbzfdService;
import com.ruoyi.common.core.text.Convert;

/**
 * 标书中标支付单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-22
 */
@Service
public class GcBszbzfdServiceImpl implements IGcBszbzfdService 
{
    @Autowired
    private GcBszbzfdMapper gcBszbzfdMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询标书中标支付单
     * 
     * @param id 标书中标支付单主键
     * @return 标书中标支付单
     */
    @Override
    public GcBszbzfd selectGcBszbzfdById(Long id)
    {
        return gcBszbzfdMapper.selectGcBszbzfdById(id);
    }

    /**
     * 查询标书中标支付单列表
     * 
     * @param gcBszbzfd 标书中标支付单
     * @return 标书中标支付单
     */
    @Override
    public List<GcBszbzfd> selectGcBszbzfdList(GcBszbzfd gcBszbzfd)
    {
        return gcBszbzfdMapper.selectGcBszbzfdList(gcBszbzfd);
    }

    /**
     * 新增标书中标支付单
     * 
     * @param gcBszbzfd 标书中标支付单
     * @return 结果
     */
    @Override
    public int insertGcBszbzfd(GcBszbzfd gcBszbzfd)
    {
    	int rows = gcBszbzfdMapper.insertGcBszbzfd(gcBszbzfd);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(gcBszbzfd.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gcBszbzfd.getOwner());
        variables.put("level1", gcBszbzfd .getZmr());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjzhongbiaofuwufei", String.valueOf(gcBszbzfd.getId()), variables);
    	
        GcBszbzfd obj = new GcBszbzfd();
    	obj.setId(gcBszbzfd.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	gcBszbzfdMapper.updateGcBszbzfd(obj);
        return rows;
    }

    /**
     * 修改标书中标支付单
     * 
     * @param gcBszbzfd 标书中标支付单
     * @return 结果
     */
    @Override
    public int updateGcBszbzfd(GcBszbzfd gcBszbzfd)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(gcBszbzfd.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", gcBszbzfd.getOwner());
//        variables.put("level1", gcBszbzfd.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjzhongbiaofuwufei", String.valueOf(gcBszbzfd.getId()), variables);
//        gcBszbzfd.setProcessInstanceId(pi.getProcessInstanceId());
        return gcBszbzfdMapper.updateGcBszbzfd(gcBszbzfd);
    }

    /**
     * 批量删除标书中标支付单
     * 
     * @param ids 需要删除的标书中标支付单主键
     * @return 结果
     */
    @Override
    public int deleteGcBszbzfdByIds(String ids)
    {
        return gcBszbzfdMapper.deleteGcBszbzfdByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除标书中标支付单信息
     * 
     * @param id 标书中标支付单主键
     * @return 结果
     */
    @Override
    public int deleteGcBszbzfdById(Long id)
    {
        return gcBszbzfdMapper.deleteGcBszbzfdById(id);
    }

	@Override
	public int updateGcBszbzfd4Check(GcBszbzfd gcBszbzfd) {
		return gcBszbzfdMapper.updateGcBszbzfd(gcBszbzfd);
	}
}
